"""
存储桶查询参数模型
"""
from typing import Optional, List
from fastapi import Query
from axiom_boot.api.filter_params import BaseFilterParams, FilterOperator


class BucketFilterParams(BaseFilterParams):
    """
    用于存储桶列表查询的过滤参数模型
    """
    display_name: Optional[str] = Query(None, description="要模糊搜索的存储桶显示名称")
    order_by: Optional[List[str]] = Query(None, description="排序字段，格式如 'create_time desc'")

    _operators = {
        "display_name": FilterOperator.LIKE
    }

    def to_filter_dict(self):
        # 排除 order_by 和所有值为 None 的字段
        data = self.model_dump(exclude_unset=True, exclude_none=True, exclude={'order_by'})
        
        filter_dict = {}
        for field, value in data.items():
            operator = self._operators.get(field, FilterOperator.EQUAL)
            key = f"{field}__{operator.value}"
            
            if operator == FilterOperator.LIKE:
                filter_dict[key] = f"%{value}%"
            else:
                filter_dict[key] = value
        
        return filter_dict
